﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Inventory{

	List<Element> lelements { get; set; }
	
	public Inventory()
	{
		lelements = new List<Element>();
	}
	public void AddElement(Element item)
	{
		if(lelements.Exists(x=>x.Name == item.Name))
			lelements.Find( x=> x.Name == item.Name).Add(item.Quantity);
		else
			lelements.Add(item);
	}
	
	private void DeleteElement(Element item)
	{
		lelements.Remove(item);
	}
	
    /// <summary>
    /// Use all the quantity for the element
    /// </summary>
    /// <param name="item"></param>
    /// <returns></returns>
	public bool UseElement(Element item)
	{
		if(lelements.Exists(x=>x.Name == item.Name)){
			lelements.Find(x=>x.Name == item.Name).Use(item.Quantity);
			return true;
		}
		return false;
	}

    /// <summary>
    /// Use a quantity for the element
    /// </summary>
    /// <param name="item"></param>
    /// <param name="iAmount">Quantity to use</param>
    /// <returns></returns>
    public bool UseElement(Element item,int iAmount)
    {
        if (lelements.Exists(x => x.Name == item.Name))
        {
            lelements.Find(x => x.Name == item.Name).Use(iAmount);
            return true;
        }
        return false;
    }
	
	public Element GetElement(string sName)
	{
		
		return lelements.Find(x=>x.Name == sName);
	}
	
	public Element GetItemByIndex(int iIndex)
	{
		return lelements[iIndex];	
	}
	
	public int GetSizeInventory()
	{
		return lelements.Count;
	}
}
